package test.tojson;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocumentList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Tojson {

	private String url;
	private SolrDocumentList Slist;
	private JSONObject json;

	public Tojson() {
		this.SetSlist(Slist);
		this.SetUrl(url);
		this.SetJson(json);
	}

	public void SetUrl(String url) {
		this.url = url;
	}

	public String GetUrl() {
		return url;
	}

	public void SetSlist(SolrDocumentList Slist) {
		this.Slist = Slist;
	}

	public SolrDocumentList GetSlist() {
		return Slist;
	}

	public void SetJson(JSONObject json) {
		this.json = json;
	}

	public JSONObject GetJson() {
		return json;
	}

	public SolrDocumentList SearchResultSolr() throws SolrServerException {

		HttpSolrServer solr = new HttpSolrServer(this.GetUrl());
		SolrQuery que = new SolrQuery();
		que.setQuery("*:*");
		que.setRequestHandler("/select");
		que.setParam("qt", "");
		que.setParam("q", "*:*");
		que.setParam("rows", "3");
		que.setParam("wt", "json");

		que.setStart(0);
		QueryResponse rest_ = solr.query(que);
		// SolrDocumentList results = rest_.getResults();
		this.SetSlist(rest_.getResults());
		SolrDocumentList results = this.GetSlist();
		return results;

	}

	public void FiletoJson() throws Exception {		

		for (int i = 0; i < this.GetSlist().size(); i++) {
			
			JSONObject json = new JSONObject(this.GetSlist().get(i));
			
			this.SetJson(json);			
			System.out.println(json.toString());
		}
	}

	public static void main(String[] arg) throws Exception {

		String url = "http://192.168.0.43:8080/solr/economy";
		Tojson tj = new Tojson();
		tj.SetUrl(url);
		tj.SearchResultSolr();
		tj.FiletoJson();
	}

}
